<?php
/**********************************************************************************
WIKINDX: Bibliographic Management system.
Copyright (C)

This program is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program;
if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

The WIKINDX Team 2007
sirfragalot@users.sourceforge.net

**********************************************************************************/
/*****
* BROWSECOMMON class
*
* Common methods for browsing as a 'tag cloud'
*
* 
$Header: /cvsroot/wikindx/wikindx3/core/browse/BROWSECOMMON.php,v 1.9 2007/07/20 10:38:09 sirfragalot Exp $
*****/
class BROWSECOMMON
{
// Constructor
	function BROWSECOMMON($db)
	{
		$this->db = $db;
		include_once("core/session/SESSION.php");
		$this->session = new SESSION();
		include_once("core/messages/MESSAGES.php");
		$this->messages = new MESSAGES();
		$this->start = $this->total = 0;
		include('config.php');
		if(!isset($WIKINDX_TAG_LOW_COLOUR))
			$this->lowColour = 'a0a0a0'; // light grey
		else
			$this->lowColour = $WIKINDX_TAG_LOW_COLOUR;
		if(!isset($WIKINDX_TAG_HIGH_COLOUR))
			$this->highColour = 'ff0000'; // red
		else
			$this->highColour = $WIKINDX_TAG_HIGH_COLOUR;
		if(!isset($WIKINDX_TAG_LOW_SIZE))
			$this->lowSize = 1;
		else
			$this->lowSize = $WIKINDX_TAG_LOW_SIZE;
		if(!isset($WIKINDX_TAG_HIGH_SIZE))
			$this->highSize = 2;
		else
			$this->highSize = $WIKINDX_TAG_HIGH_SIZE;
		$this->sizeDiff = $this->highSize - $this->lowSize;
	}
/**
* userBibCondition() - return a SQL condition clause if we are browsing a user bibliography to ensure that 
* listed, selected or searched resources come only from that user bibliography. If we are browsing the 
* master WIKINDX bibliography, return FALSE
*
* @return	SQL condition clause or FALSE
*/
	function userBibCondition($table)
	{
		$condition = $usingBib = FALSE;
		if($useBib = $this->session->getVar("mywikindx_bibliography_use"))
		{
			$recordset = $this->db->select(array('WKX_user_bibliography'), array('bibliography'), 
				" WHERE " . $this->db->formatfield('id') . "=" . $this->db->tidyInput($useBib));
			$dbBibs = $this->db->fetchOne($recordset);
			$bibsArray = explode(",", $dbBibs);
			foreach($bibsArray as $resourceId)
				$conditionArray[] = $this->db->tidyInput($resourceId);
			$condition = " ($table.id = " . implode(" OR $table.id = ", $conditionArray) . ")";
			$recordset = $this->db->select(array('WKX_user_bibliography'), array('title'), 
				" WHERE " . $this->db->formatfield('id') . "=" . $this->db->tidyInput($useBib));
			$row = $this->db->fetchRow($recordset);
			$usingBib = stripslashes($row['title']);
		}
		return array($condition, $usingBib);
	}
// For keywords in metadata
	function userBibMetaCondition($table)
	{
		$condition = $usingBib = FALSE;
		if($useBib = $this->session->getVar("mywikindx_bibliography_use"))
		{
			$recordset = $this->db->select(array('WKX_user_bibliography'), array('bibliography'), 
				" WHERE " . $this->db->formatfield('id') . "=" . $this->db->tidyInput($useBib));
			$dbBibs = $this->db->fetchOne($recordset);
			$bibsArray = explode(",", $dbBibs);
			foreach($bibsArray as $resourceId)
				$conditionArray[] = $this->db->tidyInput($resourceId);
			$condition = " ($table.resourceId = " . implode(" OR $table.resourceId = ", $conditionArray) . ")";
			$recordset = $this->db->select(array('WKX_user_bibliography'), array('title'), 
				" WHERE " . $this->db->formatfield('id') . "=" . $this->db->tidyInput($useBib));
			$row = $this->db->fetchRow($recordset);
			$usingBib = stripslashes($row['title']);
		}
		return array($condition, $usingBib);
	}
// Work out text colour (shade of red) based on field frequency.
	function colourText($lowestSum, $highestSum, $frequency)
	{
		if($frequency == $lowestSum)
			return $this->lowColour;
		if($frequency == $highestSum)
			return $this->highColour;
		$ratio = $frequency/$highestSum;
		$invRatio = 1 - $ratio;
// red
		$high = hexdec(substr($this->highColour, 0, 2));
		$low = hexdec(substr($this->lowColour, 0, 2));
		$r = round(($high * $ratio) - ($low * $invRatio));
		$r = $r < 0 ? $r * -1 : $r;
// green
		$high = hexdec(substr($this->highColour, 2, 2));
		$low = hexdec(substr($this->lowColour, 2, 2));
		$g = round(($high * $ratio) - ($low * $invRatio));
		$g = $g < 0 ? $g * -1 : $g;
// blue
		$high = hexdec(substr($this->highColour, 4, 2));
		$low = hexdec(substr($this->lowColour, 4, 2));
		$b = round(($high * $ratio) - ($low * $invRatio));
		$b = $b < 0 ? $b * -1 : $b;

		$r = $r < 16 ? '0' . dechex($r) : dechex($r);
		$g = $g < 16 ? '0' . dechex($g) : dechex($g);
		$b = $b < 16 ? '0' . dechex($b) : dechex($b);
		return "#" . $r . $g . $b;
	}
// Work out text size based on field frequency.
// Size range is 1em to 1.5em for items with $index of <= 255 and more for anything larger.
// Limit maximum size to 2;
	function sizeText($lowestSum, $highestSum, $frequency)
	{
//		if($frequency == $lowestSum)
//			return $this->lowSize;
//		if($frequency == $highestSum)
//			return $this->highSize;
		$ratio = $frequency/$highestSum;
		$add = $this->sizeDiff * $ratio;
		$size = round($this->lowSize + $add, 2);
		$size = $size < 0 ? $size * -1 : $size;
		return $size . "em";
	}
// Set the paging counter
	function setPaging($vars)
	{
		if(array_key_exists('pagingStart', $vars))
		{
			$this->session->setVar('mywikindx_pagingTagCloudStart', $vars['pagingStart']);
			$this->start = $vars['pagingStart'];
		}
		else
			$this->start = 0;
		$this->paging = $this->session->getVar("setup_pagingTagCloud");
		$this->maxLinks = $this->session->getVar('setup_pagingMaxLinks');
		$this->maxLinksHalf = round($this->maxLinks/2);
	}
// Return limited set from final array if paging required
	function paging($vars, $inputArray)
	{
		$this->setPaging($vars);
		if($this->paging <= 0) // unlimited
			return $inputArray;
		$this->total = sizeof($inputArray);
// NB - array_slice does not preserve keys until PHP  v5.0.2
		$keys = array_keys($inputArray);
		$values = array_values($inputArray);
		$keySlice = array_slice($keys, $this->start, $this->paging);
		$valueSlice = array_slice($values, $this->start, $this->paging);
// array_combine only for PHP v5
		foreach($keySlice as $key)
			$finalArray[$key] = array_shift($valueSlice);
		return $finalArray;
	}
	
// paging links if required
	function pagingLinks($queryString)
	{
		if(($this->paging <= 0) || ($this->total <= $this->paging))
			return FALSE;
		include_once("core/html/MISC.php");
		$end = $advanced = 0;
		$index = $maxLinks = 1;
		$advance = $this->start;
		while($advance >= (($this->maxLinksHalf * $this->paging) - $this->paging))
		{
			$end += $this->paging;
			$index += $this->paging;
			$advance -= $this->paging;
			$advanced++;
		}
		if($advanced)
		{
			$links[] = MISC::a("page", $this->messages->text("resources", "pagingStart"), 
				"index.php?" . htmlentities($queryString . "&pagingStart=0"));
			$maxLinks++;
		}
		while($index <= $this->total)
		{
			if($maxLinks++ >= $this->maxLinks)
				break;
			$end += $this->paging;
			if($end > $this->total)
				$end = $this->total;
			$start = $index - 1;
			$link = htmlentities($queryString . "&pagingStart=$start");
			$name = $index . " - " . $end;
			if($this->start == $start)
				$links[] = $name;
			else
				$links[] = MISC::a("page", $name, "index.php?" . $link);
			$index += $this->paging;
		}
		if($end < $this->total)
		{
			if($this->start && sizeof($links) == 1)
			{
				$links = array(MISC::a("page", $this->messages->text("resources", "pagingStart"), 
					"index.php?" . htmlentities($queryString . "&pagingStart=0")));
			}
			else if(sizeof($links) > 1)
			{
				$start = $this->total - ($this->total % $this->paging);
				if($start == $this->total)
					$start = $this->total - $this->paging;
				$links[] = MISC::a("page", $this->messages->text("resources", "pagingEnd"), 
					"index.php?" . htmlentities($queryString . "&pagingStart=$start"));
			}
		}
		return implode("&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;", $links);
	}
// Format display information string
	function linksInfo()
	{
		if($this->paging <= 0) // unlimited
			return FALSE;
		$displayEnd = $this->start + $this->paging;
		if(($this->paging <= 0) || ($displayEnd > $this->total))
			$displayEnd = $this->total;
		$displayStart = $this->start + 1;
		$bib = FALSE;
		return MISC::p($this->messages->text("hint", "pagingInfo", 
			" $displayStart - $displayEnd of " . $this->total . $bib));
		
	}
}
?>